package pers.karl.mongodb;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import pers.karl.mongodb.entity.User;

import java.util.Date;

/**
 * @author karl
 * @description: 直接访问Mongdb演示
 * @create 2019-06-09 08:10
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class MongoDbDemo {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void addUser(){
        User user = new User();
        user.setName("李四");
        user.setAge(18);
        user.setCreatedTime(new Date());
        user.setDeleted(false);
        mongoTemplate.save(user);
    }

    public void updateUser(){
//        User user = new User();
//        user.setId(new ObjectId("5cfc55ce4ca50929acc0db7d"));

//        user = mongoTemplate.updateNotNullField(user);
    }

    @Test
    public void findById(){
        User user = mongoTemplate.findById("5cfc55ce4ca50929acc0db7d", User.class);
        System.out.println(user);
    }




}
